package com.thinkgem.jeesite.modules.quartz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.modules.cn.entity.AttendanceCn;
import com.thinkgem.jeesite.modules.cn.service.AttendanceCnService;
import com.thinkgem.jeesite.modules.de.service.AttendanceDeService;
import com.thinkgem.jeesite.modules.guest.service.UsAttendanceGuestService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.us.entity.UsAttendanceAdp;
import com.thinkgem.jeesite.modules.us.service.UsAttendanceAdpService;
import com.thinkgem.jeesite.modules.warehouse.entity.BIWarehouseKpiData;
import com.thinkgem.jeesite.modules.warehouse.service.WarehouseKpiMonitoringService;
import com.thinkgem.jeesite.modules.warehousekpi.entity.WarehouseKpiReport;
import com.thinkgem.jeesite.modules.warehousekpi.service.WarehouseKpiReportService;

@Service("syncWarehouseAttendance")
public class SyncWarehouseAttendance {

	/**
	 * 日志
	 */
	private static final Log log = LogFactory
			.getLog(SyncWarehouseAttendance.class);

	/**
	 * BI报表数据
	 */
	@Autowired
	private WarehouseKpiMonitoringService warehouseKpiMonitoringService;

	/**
	 * 计算仓库BI数据
	 */
	@Autowired
	private WarehouseKpiReportService warehouseKpiReportService;

	/**
	 * 计算统一模板的考勤数据（中国，澳洲，英国，德国...）
	 */
	@Autowired
	private AttendanceCnService attendanceCnService;

	@Autowired
	private AttendanceDeService attendanceDeService;

	@Autowired
	private UsAttendanceAdpService usAttendanceAdpService;

	@Autowired
	private UsAttendanceGuestService usAttendanceGuestService;

	/**
	 * 定时计算仓库及考勤数据
	 */
	public void doSomeThing() {
		try {

			// 中国，英国，美国统一模板有效工时BI报表 工时
			 unifiedTemplate();

			// 德国
			de();

			// 美国正岗
			 usAdp();
			 
			 

		} catch (Exception e) {
			//
			log.error("考勤定时任务处理异常" + e.getMessage(), e);
		}

	}

	/**
	 * 中国，英国，美国统一模板有效工时BI报表 工时
	 */
	private void unifiedTemplate() {

		String[] str = { "UK Warehouse", "UKGF Warehouse", "AU Warehouse",
				"ShangHai Warehouse", "ShenZhen Warehouse",
				"GuangZhou Warehouse"};

		WarehouseKpiReport warehouseKpiReport;
		for (int i = 0; i < str.length; i++) {
			// 定义存储数据
			List<WarehouseKpiReport> wareList = new ArrayList<WarehouseKpiReport>();
			BIWarehouseKpiData data = new BIWarehouseKpiData();
			data.setWarehouse(str[i]);
			// 查询报表数据
			List<BIWarehouseKpiData> kpiList = warehouseKpiMonitoringService
					.syncWarehouseAttendance(data);
			// 在数据库表里面记录
			if (null != kpiList && kpiList.size() > 0) {
				for (BIWarehouseKpiData biwarehouseKpiData : kpiList) {
					if (null != biwarehouseKpiData) {
						warehouseKpiReport = new WarehouseKpiReport();
						// 设置ID
						warehouseKpiReport.setId(IdGen.uuid());
						// 仓库
						warehouseKpiReport.setWarehouse(biwarehouseKpiData
								.getWarehouse());
						// 日期
						warehouseKpiReport
								.setDate(biwarehouseKpiData.getDate());
						// 有效工时合计
						warehouseKpiReport
								.setEffectiveWorkingHours(biwarehouseKpiData
										.getEffectiveWorkingHours());
						// 标准工时
						warehouseKpiReport
								.setStandardWorkingHours(biwarehouseKpiData
										.getStandardWorkingHours());
						// 增值工时
						warehouseKpiReport
								.setValueAddedHours(biwarehouseKpiData
										.getValueAddedHours());
						// 异常工时
						warehouseKpiReport
								.setAbnormalWorkingHours(biwarehouseKpiData
										.getAbnormalWorkingHours());
						// 支持工时
						warehouseKpiReport
								.setSupportWorkingHours(biwarehouseKpiData
										.getSupportWorkingHours());
						// 获取用户信息
						User user = UserUtils.getUser();
						if (StringUtils.isNotBlank(user.getId())) {
							warehouseKpiReport.setUpdateBy(user);
							warehouseKpiReport.setCreateBy(user);
						}
						// 跟新update时间
						warehouseKpiReport.setUpdateDate(new Date());
						// 设置创建时间
						warehouseKpiReport.setCreateDate(warehouseKpiReport
								.getUpdateDate());

						warehouseKpiReport.setStunden(biwarehouseKpiData
								.getStunden());
						warehouseKpiReport
								.setEffectivePercentage(biwarehouseKpiData
										.getEffectivePercentage());
						warehouseKpiReport.setEmployeeCount(biwarehouseKpiData
								.getEmployeeCount());

						// 存数据
						wareList.add(warehouseKpiReport);
					}
				}// end for
					// 批量提交数据
				warehouseKpiReportService.batchInsert(wareList);

				// 批量修改报表数据的同步状态
				warehouseKpiMonitoringService.updateStatus(data);

				// 修改考勤的数据的状态
				AttendanceCn cn = new AttendanceCn();
				cn.setWarehouse(str[i]);
				attendanceCnService.updateStatus(cn);

			}// end if

		}// end for

	}

	/**
	 * 中国，英国，美国统一模板有效工时BI报表 工时
	 */
	private void de() {
		// 定义存储数据
		List<WarehouseKpiReport> wareList = new ArrayList<WarehouseKpiReport>();
		BIWarehouseKpiData data = new BIWarehouseKpiData();
		data.setWarehouse("DE Warehouse");
		WarehouseKpiReport warehouseKpiReport;
		// 查询报表数据
		List<BIWarehouseKpiData> kpiList = warehouseKpiMonitoringService
				.findAllListBIWarehouseKpiDE(data);

		// 在数据库表里面记录
		if (null != kpiList && kpiList.size() > 0) {
			for (BIWarehouseKpiData biwarehouseKpiData : kpiList) {
				if (null != biwarehouseKpiData) {
					warehouseKpiReport = new WarehouseKpiReport();
					// 设置ID
					warehouseKpiReport.setId(IdGen.uuid());
					// 仓库
					warehouseKpiReport.setWarehouse(biwarehouseKpiData
							.getWarehouse());
					// 日期
					warehouseKpiReport.setDate(biwarehouseKpiData.getDate());
					// 有效工时合计
					warehouseKpiReport
							.setEffectiveWorkingHours(biwarehouseKpiData
									.getEffectiveWorkingHours());
					// 标准工时
					warehouseKpiReport
							.setStandardWorkingHours(biwarehouseKpiData
									.getStandardWorkingHours());
					// 增值工时
					warehouseKpiReport.setValueAddedHours(biwarehouseKpiData
							.getValueAddedHours());
					// 异常工时
					warehouseKpiReport
							.setAbnormalWorkingHours(biwarehouseKpiData
									.getAbnormalWorkingHours());
					// 支持工时
					warehouseKpiReport
							.setSupportWorkingHours(biwarehouseKpiData
									.getSupportWorkingHours());
					// 获取用户信息
					User user = UserUtils.getUser();
					if (StringUtils.isNotBlank(user.getId())) {
						warehouseKpiReport.setUpdateBy(user);
						warehouseKpiReport.setCreateBy(user);
					}
					// 跟新update时间
					warehouseKpiReport.setUpdateDate(new Date());
					// 设置创建时间
					warehouseKpiReport.setCreateDate(warehouseKpiReport
							.getUpdateDate());

					warehouseKpiReport.setStunden(biwarehouseKpiData
							.getStunden());
					warehouseKpiReport
							.setEffectivePercentage(biwarehouseKpiData
									.getEffectivePercentage());
					warehouseKpiReport.setEmployeeCount(biwarehouseKpiData
							.getEmployeeCount());

					// 存数据
					wareList.add(warehouseKpiReport);
				}
			}
			// 批量提交数据
			warehouseKpiReportService.batchInsert(wareList);
			// 批量修改报表数据的同步状态
			warehouseKpiMonitoringService.updateStatus(data);
			// 修改考勤的数据的状态
			attendanceDeService.updateStatus();
		}// end if
	}

	/**
	 * 中国，英国，美国统一模板有效工时BI报表 工时
	 */
	private void usAdp() {

		String[] str = { "USKYN Warehouse", "USWC Warehouse" };

		WarehouseKpiReport warehouseKpiReport;
		UsAttendanceAdp adp = new UsAttendanceAdp();
		for (int i = 0; i < str.length; i++) {
			// 定义存储数据
			List<WarehouseKpiReport> wareList = new ArrayList<WarehouseKpiReport>();
			BIWarehouseKpiData data = new BIWarehouseKpiData();
			data.setWarehouse(str[i]);
			// 修改考勤的数据的状态 美国仓库 R8F67=USKY RV6CU=USWC
			if ("USKYN Warehouse".equals(str[i])) {
				adp.setCompanyCode("R8F67");
				adp.setWarehouse("USKYN Warehouse");
				data.setCompanyCode("R8F67");
				data.setWarehouse("USKYN Warehouse");
			} else if ("USWC Warehouse".equals(str[i])) {
				adp.setWarehouse("USWC Warehouse");
				adp.setCompanyCode("RV6CU");
				data.setCompanyCode("RV6CU");
				data.setWarehouse("USWC Warehouse");
			}

			// 查询报表数据
			List<BIWarehouseKpiData> kpiList = usAttendanceAdpService
					.syncUsAttendanceAdp(data);

			// 在数据库表里面记录
			if (null != kpiList && kpiList.size() > 0) {
				for (BIWarehouseKpiData biwarehouseKpiData : kpiList) {
					if (null != biwarehouseKpiData) {
						warehouseKpiReport = new WarehouseKpiReport();
						// 设置ID
						warehouseKpiReport.setId(IdGen.uuid());
						// 仓库
						warehouseKpiReport.setWarehouse(biwarehouseKpiData
								.getWarehouse());
						// 日期
						warehouseKpiReport
								.setDate(biwarehouseKpiData.getDate());
						// 有效工时合计
						warehouseKpiReport
								.setEffectiveWorkingHours(biwarehouseKpiData
										.getEffectiveWorkingHours());
						// 标准工时
						warehouseKpiReport
								.setStandardWorkingHours(biwarehouseKpiData
										.getStandardWorkingHours());
						// 增值工时
						warehouseKpiReport
								.setValueAddedHours(biwarehouseKpiData
										.getValueAddedHours());
						// 异常工时
						warehouseKpiReport
								.setAbnormalWorkingHours(biwarehouseKpiData
										.getAbnormalWorkingHours());
						// 支持工时
						warehouseKpiReport
								.setSupportWorkingHours(biwarehouseKpiData
										.getSupportWorkingHours());
						// 获取用户信息
						User user = UserUtils.getUser();
						if (StringUtils.isNotBlank(user.getId())) {
							warehouseKpiReport.setUpdateBy(user);
							warehouseKpiReport.setCreateBy(user);
						}
						// 跟新update时间
						warehouseKpiReport.setUpdateDate(new Date());
						// 设置创建时间
						warehouseKpiReport.setCreateDate(warehouseKpiReport
								.getUpdateDate());

						warehouseKpiReport.setStunden(biwarehouseKpiData
								.getStunden());
						warehouseKpiReport
								.setEffectivePercentage(biwarehouseKpiData
										.getEffectivePercentage());
						warehouseKpiReport.setEmployeeCount(biwarehouseKpiData
								.getEmployeeCount());

						// 存数据
						wareList.add(warehouseKpiReport);
					}
				}// end for

				// 批量提交数据
				warehouseKpiReportService.batchInsert(wareList);

				// 批量修改报表数据的同步状态
				warehouseKpiMonitoringService.updateStatus(data);

				// 修改考勤的数据的状态 美国仓库 R8F67=USKY RV6CU=USWC
				usAttendanceAdpService.updateStatus(adp);
				
				//外包员工数据处理
				AttendanceCn cn = new AttendanceCn();
				cn.setWarehouse(str[i]);
				attendanceCnService.updateStatus(cn);

			}// end if
		}// end for
	}

	// /**
	// * 中国，英国，美国统一模板有效工时BI报表 工时
	// */
	// private void unifiedTemplate() {
	//
	// // 中国，英国，美国统一模板有效工时BI报表 工时
	// // 查询没有计算的考勤记录，状态为：1
	// List<WarehouseKpiReport> list = warehouseKpiReportService
	// .findList(new WarehouseKpiReport());
	// // 查询考勤数据
	// List<AttendanceCn> syncAttList = attendanceCnService.syncAttendance();
	// if (null != list && list.size() > 0) {
	// SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
	// List<WarehouseKpiReport> reportList = new
	// ArrayList<WarehouseKpiReport>();
	// List<AttendanceCn> attList = new ArrayList<AttendanceCn>();
	// for (WarehouseKpiReport wa : list) {
	// if (null != wa) {
	// // 查找对应的数据，例如：时间一致，仓库一致
	// for (AttendanceCn at : syncAttList) {
	// String stra = fmt.format(wa.getDate());
	// String strat = fmt.format(at.getDate());
	// if (null == at.getDate()) {
	// continue;
	// }
	// // 时间一致及仓库一致
	// if (stra.equals(strat)
	// && wa.getWarehouse().equals(at.getWarehouse())) {
	// String workingHours = at.getWorkingHours();
	// String effectiveWorkingHours = wa
	// .getEffectiveWorkingHours();
	// Double douWorkingHours = Double
	// .valueOf(workingHours);
	// Double doueffectiveWorkingHours = Double
	// .valueOf(effectiveWorkingHours);
	// Double douValue = doueffectiveWorkingHours
	// / douWorkingHours;
	// wa.setStunden(String.valueOf(douValue));
	// wa.setEmployeeCount(at.getEmployeeCount());
	// // 修改考勤状态
	// at.setStatus("2");
	// attList.add(at);
	// break;
	// }// end if
	// }// end for
	//
	// if (null != wa.getStunden()
	// && null != wa.getEmployeeCount()) {
	// wa.setStatus("2");
	// reportList.add(wa);
	// }
	// }// end if
	// }// end for
	//
	// if (reportList.size() > 0) {
	// // 修改报表数据
	// warehouseKpiReportService.batchupdate(reportList);
	// }
	//
	// if (attList.size() > 0) {
	// // 修改考勤状态
	// attendanceCnService.batchupdate(attList);
	// }
	//
	// }// end if
	// }
	//
	// /**
	// * 德国
	// */
	// private void de() {
	//
	// // 中国，英国，美国统一模板有效工时BI报表 工时
	// // 查询没有计算的考勤记录，状态为：1
	// List<WarehouseKpiReport> list = warehouseKpiReportService
	// .findList(new WarehouseKpiReport());
	// // 查询考勤数据
	// List<AttendanceDe> syncAttList = attendanceDeService.syncAttendanceDe();
	// if (null != list && list.size() > 0) {
	// SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
	// List<WarehouseKpiReport> reportList = new
	// ArrayList<WarehouseKpiReport>();
	// List<AttendanceDe> attList = new ArrayList<AttendanceDe>();
	// for (WarehouseKpiReport wa : list) {
	// if (null != wa) {
	// // 查找对应的数据，例如：时间一致，仓库一致
	// for (AttendanceDe at : syncAttList) {
	// String stra = fmt.format(wa.getDate());
	// String strat = fmt.format(at.getDatum());
	// if (null == at.getDatum()) {
	// continue;
	// }
	// // 时间一致及仓库一致
	// if (stra.equals(strat)
	// && "DE Warehouse".equals(wa.getWarehouse())) {
	// String workingHours = at.getStunden();
	// String effectiveWorkingHours = wa
	// .getEffectiveWorkingHours();
	// Double douWorkingHours = Double
	// .valueOf(workingHours);
	// Double doueffectiveWorkingHours = Double
	// .valueOf(effectiveWorkingHours);
	// Double douValue = doueffectiveWorkingHours
	// / douWorkingHours;
	// wa.setStunden(String.valueOf(douValue));
	// wa.setEmployeeCount(at.getEmployeeCount());
	// // 修改考勤状态
	// at.setStatus("2");
	// attList.add(at);
	// break;
	// }// end if
	// }// end for
	// if (null != wa.getStunden()
	// && null != wa.getEmployeeCount()) {
	// wa.setStatus("2");
	// reportList.add(wa);
	// }
	// }// end if
	// }// end for
	//
	// if (reportList.size() > 0) {
	// // 修改报表数据
	// warehouseKpiReportService.batchupdate(reportList);
	// }
	// if (attList.size() > 0) {
	// // 修改考勤状态
	// attendanceDeService.batchupdate(attList);
	// }
	// }// end if
	// }
	//
	// /**
	// * 美国正岗
	// */
	// private void usAdp() {
	// // 中国，英国，美国统一模板有效工时BI报表 工时
	// // 查询没有计算的考勤记录，状态为：1
	// List<WarehouseKpiReport> list = warehouseKpiReportService
	// .findList(new WarehouseKpiReport());
	// // 查询考勤数据
	// List<UsAttendanceAdp> syncAttList = usAttendanceAdpService
	// .syncUsAttendanceAdp();
	// if (null != list && list.size() > 0) {
	// SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
	// List<WarehouseKpiReport> reportList = new
	// ArrayList<WarehouseKpiReport>();
	// List<UsAttendanceAdp> attList = new ArrayList<UsAttendanceAdp>();
	// for (WarehouseKpiReport wa : list) {
	// if (null != wa) {
	// // 查找对应的数据，例如：时间一致，仓库一致
	// for (UsAttendanceAdp at : syncAttList) {
	// String stra = fmt.format(wa.getDate());
	// String strat = fmt.format(at.getInTime());
	// if (null == at.getInTime()) {
	// continue;
	// }
	// // 时间一致及仓库一致
	// if (stra.equals(strat)) {
	// // R8F67=USKY RV6CU=USWC
	// if ("USWC Warehouse".equals(wa.getWarehouse())
	// && "RV6CU".equals(at.getCompanyCode())) {
	// String workingHours = at.getStunden();
	// String effectiveWorkingHours = wa
	// .getEffectiveWorkingHours();
	// Double douWorkingHours = Double
	// .valueOf(workingHours);
	// Double doueffectiveWorkingHours = Double
	// .valueOf(effectiveWorkingHours);
	// Double douValue = doueffectiveWorkingHours
	// / douWorkingHours;
	// wa.setStunden(String.valueOf(douValue));
	// wa.setEmployeeCount(at.getEmployeeCount());
	// // 修改考勤状态
	// at.setStatus("2");
	// attList.add(at);
	// break;
	// } else if ("USKY Warehouse".equals(wa
	// .getWarehouse())
	// && "R8F67".equals(at.getCompanyCode())) {
	// String workingHours = at.getStunden();
	// String effectiveWorkingHours = wa
	// .getEffectiveWorkingHours();
	// Double douWorkingHours = Double
	// .valueOf(workingHours);
	// Double doueffectiveWorkingHours = Double
	// .valueOf(effectiveWorkingHours);
	// Double douValue = doueffectiveWorkingHours
	// / douWorkingHours;
	// wa.setStunden(String.valueOf(douValue));
	// wa.setEmployeeCount(at.getEmployeeCount());
	// // 修改考勤状态
	// at.setStatus("2");
	// attList.add(at);
	// break;
	// }
	//
	// }// end if
	// }// end for
	// if (null != wa.getStunden()
	// && null != wa.getEmployeeCount()) {
	// wa.setStatus("2");
	// reportList.add(wa);
	// }
	// }// end if
	// }// end for
	//
	// if (reportList.size() > 0) {
	// // 修改报表数据
	// warehouseKpiReportService.batchupdate(reportList);
	// }
	// if (attList.size() > 0) {
	// // 修改考勤状态
	// usAttendanceAdpService.batchupdate(attList);
	// }
	// }// end if
	// }
}
